using UnityEngine;
using UnityEditor;
using System.IO;
using UndergroundSurvival.Rooms;

namespace UndergroundSurvival.Editor
{
    public class RoomPrefabCreator : EditorWindow
    {
        private string prefabName = "NewRoom";
        private string prefabPath = "Assets/Prefabs/Rooms";
        private Sprite roomSprite;
        private Sprite previewSprite;
        private Vector2 roomSize = new Vector2(3, 2);
        private Color roomColor = Color.white;
        private bool createCollider = true;
        private bool createPreview = true;

        [MenuItem("Tools/Room Prefab Creator")]
        public static void ShowWindow()
        {
            GetWindow<RoomPrefabCreator>("Room Prefab Creator");
        }

        private void OnGUI()
        {
            GUILayout.Label("Room Prefab Creator", EditorStyles.boldLabel);

            prefabName = EditorGUILayout.TextField("Prefab Name", prefabName);
            prefabPath = EditorGUILayout.TextField("Prefab Path", prefabPath);
            roomSprite = (Sprite)EditorGUILayout.ObjectField("Room Sprite", roomSprite, typeof(Sprite), false);
            previewSprite = (Sprite)EditorGUILayout.ObjectField("Preview Sprite", previewSprite, typeof(Sprite), false);
            roomSize = EditorGUILayout.Vector2Field("Room Size", roomSize);
            roomColor = EditorGUILayout.ColorField("Room Color", roomColor);
            createCollider = EditorGUILayout.Toggle("Create Collider", createCollider);
            createPreview = EditorGUILayout.Toggle("Create Preview", createPreview);

            if (GUILayout.Button("Create Prefab"))
            {
                CreateRoomPrefab();
            }
        }

        private void CreateRoomPrefab()
        {
            // 创建游戏对象
            GameObject roomObject = new GameObject(prefabName);
            
            // 添加组件
            Room room = roomObject.AddComponent<Room>();
            SpriteRenderer spriteRenderer = roomObject.AddComponent<SpriteRenderer>();
            spriteRenderer.sprite = roomSprite;
            spriteRenderer.color = roomColor;
            spriteRenderer.sortingOrder = 1;

            if (createCollider)
            {
                BoxCollider2D collider = roomObject.AddComponent<BoxCollider2D>();
                collider.size = roomSize;
            }

            if (createPreview)
            {
                GameObject previewObject = new GameObject("Preview");
                previewObject.transform.SetParent(roomObject.transform);
                previewObject.transform.localPosition = Vector3.zero;

                SpriteRenderer previewRenderer = previewObject.AddComponent<SpriteRenderer>();
                previewRenderer.sprite = previewSprite;
                previewRenderer.color = new Color(1, 1, 1, 0.5f);
                previewRenderer.sortingOrder = 2;
                previewRenderer.enabled = false;

                // 设置预览引用
                room.SetPreviewSprite(previewRenderer);
            }

            // 确保目录存在
            if (!Directory.Exists(prefabPath))
            {
                Directory.CreateDirectory(prefabPath);
            }

            // 创建预制体
            string fullPath = Path.Combine(prefabPath, prefabName + ".prefab");
            PrefabUtility.SaveAsPrefabAsset(roomObject, fullPath);
            DestroyImmediate(roomObject);

            Debug.Log($"Room prefab created at: {fullPath}");
        }
    }
} 